package com.boarsoft.boar.sequence.info.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.common.Constants;
import com.boarsoft.boar.sequence.biz.SequenceInfoBiz;
import com.boarsoft.boar.sequence.entity.SequenceInfo;
import com.boarsoft.common.Authorized;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;

@Component
@RestController
public class SequenceInfoController {
	private static final Logger log = LoggerFactory.getLogger(SequenceInfoController.class);

	@Autowired
	private SequenceInfoBiz sequenceInfoBiz;

	@Authorized(code = "sequence.info.save")
	@RequestMapping("/sequence/info/save.do")
	public ReplyInfo<Object> save(SequenceInfo form) {
		SequenceInfo o = null;
		if (Util.strIsEmpty(form.getId())) {
			o = form;
		} else {
			o = sequenceInfoBiz.get(form.getId());
			o.update(o);
		}
		try {
			sequenceInfoBiz.save(o);
			return new ReplyInfo<Object>();
		} catch (Exception e) {
			log.error("Error on save sequence {}", o, e);
			return new ReplyInfo<Object>(Constants.ERROR_UNKNOWN, e.getMessage());
		}
	}

	@Authorized(code = "sequence.info.delete")
	@RequestMapping("/sequence/info/delete.do")
	public ReplyInfo<Object> delete(String id) {
		if (Util.strIsEmpty(id)) {
			return new ReplyInfo<Object>(Constants.WARN_INVALID, "delete");
		}
		sequenceInfoBiz.delete(id);
		return new ReplyInfo<Object>();
	}

	@Authorized(code = "sequence.info.get")
	@RequestMapping("/sequence/info/get.do")
	public ReplyInfo<Object> get(String id) {
		SequenceInfo o = sequenceInfoBiz.get(id);
		return new ReplyInfo<Object>(true, o);
	}

	@Authorized(code = "sequence.info.list")
	@RequestMapping("/sequence/info/list.do")
	public ReplyInfo<Object> list(String catalog, String key, int pageNo, int pageSize, String orderBy) {
		PagedResult<SequenceInfo> pr = sequenceInfoBiz.list(catalog, key, orderBy, pageNo, pageSize);
		// for (SequenceInfo o : pr.getList()) {
		// o.setOwner(this.getSysUser(o.getOwnerId()));
		// }
		return new ReplyInfo<Object>(true, pr);
	}

	// @Cached(group = "user")
	// public SysUser getSysUser(String id) {
	// return userSvc.get(id);
	// }
}
